/*
This file generates the results on the risk-taking channel of monetary policy
see Appendix D:

	- Table E.7: Risk-taking in the corporate credit market
	- Table E.8: Risk-taking in the consumer credit market
	- Table E.9: Future delinquencies
	
*/

/////////////////////////////////////////////////////////////////////////////////
///
/// 					Corporate credit market 
///
/////////////////////////////////////////////////////////////////////////////////


use   "${procdata}/URTEVIRK_FIRE_FIRM_03-18.dta", clear 


/////////////////////////////////////////////////////////////////////////////////
/// Table E.7: Risk-taking in the corporate credit market
/////////////////////////////////////////////////////////////////////////////////


/// Compute risk-indicators for the subsample of borrowers with bank and nonbank lenders

* Mark borrowers with bank and nonbank lenders
xtset id_borrowerlender year
local regspec inter1-inter5, noconstant a(id_borrower#year id_lender) vce(cluster id_lender#id_borrower)
qui reghdfe intrate `regspec'
qui gen sample_rate = 1 if e(sample)
qui reghdfe lndebt `regspec'
qui gen sample_debt = 1 if e(sample)
gegen sample_reg = max(sample_rate sample_debt), by(id_borrower year)


* Compute indicators for high leverage and sales, and past delinquency
gen leverage = (GF_AT - GF_EGUL) / GF_AT if GF_EGUL >= 0 & !mi(GF_EGUL) 

preserve 
gcollapse (max) delinquency sample_reg (last) leverage GF_OMS, by(id_borrower year)
xtset id_borrower year

gen L_delinquency = L.delinquency

egen median_lev = median(leverage) if sample_reg == 1
gen byte high_lev = 1 if leverage > median_lev & leverage != . & sample_reg == 1 
replace high_lev = 0 if leverage <= median_lev & leverage != . & sample_reg == 1 

egen median_Sales = median(GF_OMS) if sample_reg == 1
gen byte high_Sales = 1 if GF_OMS > median_Sales & GF_OMS != . & sample_reg == 1 
replace high_Sales = 0 if GF_OMS <= median_Sales & GF_OMS != . & sample_reg == 1 

tempfile delinq
save `delinq'
restore 

merge m:1 id_borrower year using `delinq', keep(match master) nogen


* Run risk-taking regressions 	
xtset id_borrowerlender year	

eststo clear		
local i=1

foreach interaction in  L.high_lev L.high_Sales L_delinquency {	
	
	local macrovars  JK_mpshocksign DK_real_gdp_grate DK_gdp_growth_forecast ///
		DK_inflation_rate vix
		
	local RHS i.nonbank_lender##cL.(`macrovars')##i.`interaction'
	local reg_opts noconstant a(id_borrower#year id_lender) vce(cluster id_borrower#id_lender)

	eststo lndebt_`i': 	qui reghdfe lndebt 	`RHS', `reg_opts'
	eststo intrate_`i': qui reghdfe intrate `RHS', `reg_opts'

	local ++i
}		

qui estadd local MacroControlsInt "Yes": *
qui estadd local LowerInter "Yes": *
qui estadd local BorrowerYearFE "Yes": *
qui estadd local LenderFE "Yes": *

local stats_TestFE N r2 MacroControlsInt LowerInter LenderFE  BorrowerYearFE  
local label_TestFE `""Observations" "R2" "Macro Var. Interactions"  "Lower-lvl interactions" "Lender FE" "Borrower-Year FE"   "'

local keeplist  1.nonbank_lender#1*#cL.JK_mpshocksign 1.nonbank_lender#cL.JK_mpshocksign
local varlabels_RiskTaking ///
	1.nonbank_lender#cL.JK_mpshocksign "Nonbank x MP Shock" ///
	1.nonbank_lender#1L.high_lev#cL.JK_mpshocksign  "Triple - Leverage" ///
	1.nonbank_lender#1L.high_Sales#cL.JK_mpshocksign  "Triple - Sales" ///
	1.nonbank_lender#1.L_delinquency#cL.JK_mpshocksign  "Triple - Past delinquency" 
		
		
esttab *, varwidth(25) star(* 0.10 ** 0.05 *** 0.01) ///
		keep(`keeplist')  transform((exp(@)-1)*100 100*exp(@), pattern(1 0 1 0 1 0)) /// 
		cells(b(star fmt(2)) se(fmt(2) par)) varlabels(`varlabels_RiskTaking')  ///
		stats(`stats_TestFE', labels(`label_TestFE')  fmt(%9.0fc 2)) collabels(none)

	
esttab * using "${tables}/FIRMS_RiskTaking.tex", ///
		booktabs  varwidth(25) keep(`keeplist') star(* 0.10 ** 0.05 *** 0.01) /// 
		transform((exp(@)-1)*100 100*exp(@), pattern(1 0 1 0 1 0)) ///
		cells(b(star fmt(2)) se(fmt(2) par)) varlabels(`varlabels_RiskTaking')  ///
		stats(`stats_TestFE', labels(`label_TestFE')  fmt(%9.0fc 2)) collabels(none) replace

		
/////////////////////////////////////////////////////////////////////////////////
/// Table E.9.a:  Future delinquencies (corporate)
/////////////////////////////////////////////////////////////////////////////////

eststo clear		

local i=1
foreach LHS in delinquency F.delinquency {	
	
	local macrovars  JK_mpshocksign DK_real_gdp_grate DK_gdp_growth_forecast ///
		DK_inflation_rate vix
		
	local RHS i.nonbank_lender##cL.(`macrovars')
	local reg_opts noconstant  vce(cluster id_borrower#id_lender)

	eststo KM_`i': 	qui reghdfe `LHS' `RHS', `reg_opts' a(id_borrower#year id_lender)
	eststo ILST_`i': qui reghdfe `LHS' `RHS', `reg_opts' a(ILST_FE id_lender)

	local ++i
}		


qui estadd local MacroControlsInt "Yes": *
qui estadd local BorrowerYearFE "Yes": KM_*
qui estadd local LenderFE "Yes": *
qui estadd local ILSTFE "Yes": ILST_*


local stats_TestFE N r2 MacroControlsInt LenderFE  BorrowerYearFE  ILSTFE
local label_TestFE `""Observations" "R2" "Macro Var. Interactions"   "Lender FE" "Borrower-Year FE" "ILST FE"   "'
local result_list KM_* ILST_*

esttab `result_list', booktabs  varwidth(25) collabels(none) ///
		cells(b(star fmt(2)) se(fmt(2) par)) ///
		varlabels(1.nonbank_lender#cL.JK_mpshocksign "Nonbank x MP Shock")  ///
		stats(`stats_TestFE', labels(`label_TestFE')  fmt(%9.0fc 2)) ///
		mtitles("Delinq\_t" "Delinq\_\{t+1\}" ///
		"Delinq\_t" "Delinq\_\{t+1\}")  keep(1.nonbank_lender#cL.JK_mpshocksign)

* Write results to .tex file and finalize the table later with household results 
esttab `result_list' using "$tables/Regression_FutureDelinquencies.tex", ///
		booktabs  varwidth(25) collabels(none) fragment /// 
		prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{4}{c}} \toprule") ///
		posthead("\hline \\ \multicolumn{3}{l}{\textbf{A. Corporate lending}} \\\\[-1ex]") ///
		cells(b(star fmt(3)) se(fmt(3) par)) ///
		keep(1.nonbank_lender#cL.JK_mpshocksign) ///
		varlabels(1.nonbank_lender#cL.JK_mpshocksign "Nonbank x MP Shock")  ///
		mtitles("Delinq\_t" "Delinq\_\{t+1\}" ///
		"Delinq\_t" "Delinq\_\{t+1\}")   ///
		stats(N r2, labels("Observations" "R2") fmt(%12.0gc 2)) replace
		
		

/////////////////////////////////////////////////////////////////////////////////
///
/// 					Consumer credit market 
///
/////////////////////////////////////////////////////////////////////////////////		
		
use  "$procdata/urtepers_panel_ALL.dta", clear 	

cap rename delinquincy delinquency

/////////////////////////////////////////////////////////////////////////////////
/// Table E.8: Risk-taking in the consumer credit market
/////////////////////////////////////////////////////////////////////////////////

/// Compute risk-indicators for the subsample of borrowers with bank and nonbank lenders

* Mark borrowers with bank and nonbank lenders
xtset id_borrowerlender year
local regspec inter1-inter5, noconstant a(id_borrower#year id_lender) vce(cluster id_lender#id_borrower)
qui reghdfe intrate `regspec'
qui gen sample_rate = 1 if e(sample)
qui reghdfe lndebt `regspec'
qui gen sample_debt = 1 if e(sample)
gegen sample_reg = max(sample_rate sample_debt), by(id_borrower year)


* Compute indicators for high leverage and sales, and past unemployment status

preserve 
gcollapse (max) delinquency sample_reg (last) fambelaaningsgrad ///
	famdispon_13 famledig24mnd, by(id_borrower year)
	
xtset id_borrower year

egen median_lev = median(fambelaaningsgrad) if sample_reg == 1
gen byte high_lev = 1 if fambelaaningsgrad > median_lev & fambelaaningsgrad != . & sample_reg == 1 
replace high_lev = 0 if fambelaaningsgrad <= median_lev & fambelaaningsgrad != . & sample_reg == 1 

egen median_Inc = median(famdispon_13) if sample_reg == 1
gen byte high_Inc = 1 if famdispon_13 > median_Inc & famdispon_13 != . & sample_reg == 1 
replace high_Inc = 0 if famdispon_13 <= median_Inc & famdispon_13 != . & sample_reg == 1 

gen unemployment = famledig24mnd

tempfile delinq
save `delinq'
restore 

merge m:1 id_borrower year using `delinq', keep(match master) nogen


* Run risk-taking regressions 	
xtset id_borrowerlender year	

eststo clear		
local i=1

foreach interaction in  L.high_lev L.high_Inc L.unemployment {	
	
	local macrovars  JK_mpshocksign DK_real_gdp_grate DK_gdp_growth_forecast ///
		DK_inflation_rate vix
		
	local RHS i.nonbank_lender##cL.(`macrovars')##i.`interaction'
	local reg_opts noconstant a(id_borrower#year id_lender) vce(cluster id_borrower#id_lender)

	eststo lndebt_`i': 	qui reghdfe lndebt 	`RHS', `reg_opts'
	eststo intrate_`i': qui reghdfe intrate `RHS', `reg_opts'

	local ++i
}		

qui estadd local MacroControlsInt "Yes": *
qui estadd local LowerInter "Yes": *
qui estadd local BorrowerYearFE "Yes": *
qui estadd local LenderFE "Yes": *

local stats_TestFE N r2 MacroControlsInt LowerInter LenderFE  BorrowerYearFE  
local label_TestFE `""Observations" "R2" "Macro Var. Interactions"  "Lower-lvl interactions" "Lender FE" "Borrower-Year FE"   "'

local keeplist  1.nonbank_lender#1*#cL.JK_mpshocksign 1.nonbank_lender#cL.JK_mpshocksign
local varlabels_RiskTaking ///
	1.nonbank_lender#cL.JK_mpshocksign "Nonbank x MP Shock" ///
	1.nonbank_lender#1L.high_lev#cL.JK_mpshocksign  "Triple - Leverage" ///
	1.nonbank_lender#1L.high_Inc#cL.JK_mpshocksign  "Triple - Income" ///
	1.nonbank_lender#1L.unemployment#cL.JK_mpshocksign  "Triple - Past unemployment" 
		
		
esttab *, varwidth(25) star(* 0.10 ** 0.05 *** 0.01) ///
		keep(`keeplist')  transform((exp(@)-1)*100 100*exp(@), pattern(1 0 1 0 1 0)) /// 
		cells(b(star fmt(2)) se(fmt(2) par)) varlabels(`varlabels_RiskTaking')  ///
		stats(`stats_TestFE', labels(`label_TestFE')  fmt(%9.0fc 2)) collabels(none)

	
esttab * using "${tables}/PERS_RiskTaking.tex", ///
		booktabs  varwidth(25) keep(`keeplist') star(* 0.10 ** 0.05 *** 0.01) /// 
		transform((exp(@)-1)*100 100*exp(@), pattern(1 0 1 0 1 0)) ///
		cells(b(star fmt(2)) se(fmt(2) par)) varlabels(`varlabels_RiskTaking')  ///
		stats(`stats_TestFE', labels(`label_TestFE')  fmt(%12.0fc 2)) collabels(none) replace

		
		
		
/////////////////////////////////////////////////////////////////////////////////
/// Table E.9.b:  Future delinquencies (households)
/////////////////////////////////////////////////////////////////////////////////

eststo clear		

local i=1
foreach LHS in delinquency F.delinquency {	
	
	local macrovars  JK_mpshocksign DK_real_gdp_grate DK_gdp_growth_forecast ///
		DK_inflation_rate vix
		
	local RHS i.nonbank_lender##cL.(`macrovars')
	local reg_opts noconstant  vce(cluster id_borrower#id_lender)

	eststo KM_`i': 	qui reghdfe `LHS' `RHS', `reg_opts' a(id_borrower#year id_lender)
	eststo ILST_`i': qui reghdfe `LHS' `RHS', `reg_opts' a(muni_inc_lev_year_fe id_lender)

	local ++i
}		


qui estadd local MacroControlsInt "Yes": *
qui estadd local BorrowerYearFE "Yes": KM_*
qui estadd local LenderFE "Yes": *
qui estadd local ILSTFE "Yes": ILST_*


local stats_TestFE N r2 MacroControlsInt LenderFE  BorrowerYearFE  ILSTFE
local label_TestFE `""Observations" "R2" "Macro Var. Interactions"   "Lender FE" "Borrower-Year FE" "ILST FE"   "'
local result_list KM_* ILST_*

esttab `result_list', booktabs  varwidth(25) collabels(none) ///
		cells(b(star fmt(2)) se(fmt(2) par)) ///
		varlabels(1.nonbank_lender#cL.JK_mpshocksign "Nonbank x MP Shock")  ///
		stats(`stats_TestFE', labels(`label_TestFE')  fmt(%9.0fc 2)) ///
		mtitles("Delinq\_t" "Delinq\_\{t+1\}" ///
		"Delinq\_t" "Delinq\_\{t+1\}")  keep(1.nonbank_lender#cL.JK_mpshocksign)

* Add as panel b to .tex file first generated above
esttab `result_list' using "$tables/Regression_FutureDelinquencies.tex", ///
		booktabs  varwidth(25) collabels(none)  /// 
		fragment append nomtitles nonumbers nolines ///
		posthead("\\ \multicolumn{3}{l}{\textbf{B. Consumer lending}} \\\\[-1ex]") ///
		cells(b(star fmt(3)) se(fmt(3) par)) ///
		keep(1.nonbank_lender#cL.JK_mpshocksign) ///
		varlabels(1.nonbank_lender#cL.JK_mpshocksign "Nonbank x MP Shock")  ///
		postfoot("\hline\hline \end{tabular} }") ///
		stats(`stats_TestFE', labels(`label_TestFE') fmt(%12.0fc 2))
